import { makeAutoObservable, runInAction } from 'mobx'
import { getData, postData, putData } from '@/servers/http'
import { api } from '@/servers/api'
import { Dialog, Toast } from 'antd-mobile'
import { history } from 'umi'
import wx from 'weixin-webview-jssdk'
import { getLocalStorage } from '@/storage'

class Store {
    barriersInfo: any = []
    /** 是否弹第一个弹窗 */
    isFirstView: boolean = false
    /** 是否已完成实训 */
    isFinish = false

    constructor() {
        makeAutoObservable(this)
    }
    showModal = (id: string) => {
        if (this.isFirstView) {
            Dialog.confirm({
                content: '已生成的我的求职档案',
                cancelText: '好的',
                confirmText: '前往档案',
                onConfirm: () => {
                    history.replace({
                        pathname: '/archivedetail',
                        query: { id },
                    })
                },
            })
        } else {
            Dialog.confirm({
                content: '同步我的实训结果生成档案',
                cancelText: '取消',
                confirmText: '同步档案',
                onConfirm: async () => {
                    let result
                    if (this.isFinish) {
                        result = await putData(`${api.userBarriersBatchUpdate}${id}`, { fill_type: 3 })
                    } else {
                        result = await postData(`${api.userBarriersBatchInput}${id}`, { fill_type: 3 })
                    }
                    if (result.status === 200) {
                        history.replace({
                            pathname: '/archivedetail',
                            query: { id },
                        })
                    }
                },
            })
        }
    }
    /**
     * 展示弹窗
     * @param id
     * @param from 来源 wechat-微信
     */
    showSelectModal = (id: string, from: string) => {
        if (from === 'wechat') return
        const identityType = getLocalStorage('IDENTITY_TYPE')
        if (Number(identityType) === 2) {
            this.showModal(id)
        } else {
            getData(api.userBarriersScoreInfo).then(res => {
                if (Number(res.data?.is_score) === 1) {
                    Toast.show(Number(res.data?.is_course_score) === 1 ? '讲师已评分' : '讲师已评审')
                    return
                } else {
                    this.showModal(id)
                }
            })
        }
    }

    /** 获取意向岗位列表 */
    getBarriersInfo = (callback: (arg0: any) => void) => {
        const identityType = getLocalStorage('IDENTITY_TYPE')
        getData(api.jobExpects).then(({ data }) => {
            callback?.(data)
            runInAction(() => {
                this.barriersInfo = data?.job_expect_list || []
                this.isFirstView = data?.is_first_view === '1' ? true : false
                this.isFinish = data?.is_finish === '1' ? true : false

                if (Number(identityType) !== 2 && data?.gain_medal) {
                    wx.miniProgram.navigateTo({
                        url: `/pageStudent/pages/gotMedal/index?medalData=${encodeURIComponent(JSON.stringify(data.gain_medal))}`,
                    })
                }
            })
        })
    }

    resetBarriers = () => {
        this.barriersInfo = []
        this.isFirstView = false
        this.isFinish = false
    }
}
export default new Store()
